<?php
class Application_Model_DbTable_Event extends Zend_Db_Table_Abstract
{
    protected $_name = 'event';
    protected $_dependentTables = array('Application_Model_DbTable_Bet');
	protected $_referenceMap = array(
		'Tournament' => array(
			'columns' => 'tourn_id',
			'refTableClass' => 'Application_Model_DbTable_Tournament',
			'refColumns' => array('id')
		),
		'Team_Left' => array(
			'columns' => 'left_id',
			'refTableClass' => 'Application_Model_DbTable_Team',
			'refColumns' => array('id')
		),
		'Team_Right' => array(
			'columns' => 'right_id',
			'refTableClass' => 'Application_Model_DbTable_Team',
			'refColumns' => array('id')
		)
	);
	public function __construct(){
		$this->setRowClass('App_Db_Row_Event');
		parent::__construct();
	}
	public function GetEventsById($event_id = null, 
									$tourn_id = null, 
									$left_id = null, 
									$right_id = null, 
									$limit = null,
									$visible = true)
	{
    	$select = $this->select();
    	if($event_id){
    		$select->where('id = ?', (int)$event_id);
    		//return $this->fetchRow($select);
    	}
    	if($tourn_id){
    		$select->where('tourn_id = ?', (int)$tourn_id);
    	}
        if($left_id){
    		$select->where('left_id = ?', (int)$left_id);
    	}
	    if($right_id){
    		$select->where('right_id = ?', (int)$right_id);
    	}
	    if($visible){
	    	$select->where('visible = ?', 1);
    	}
    	if($limit){
    		$select->limit($limit);
    	}
    	$select->order('date_start DESC');
    	$res = $this->fetchAll($select);
		if(!$res){
    		throw new Zend_Db_Table_Exception('Cant find event', null);
    	}
    	return $res;
    }
	public function CheckDate($id){
    	$_id = (int)$id;
    	$select = $this->select();
    	$select->from('event', 'date_close');
    	$select->where('id = ?', $_id);
    	$res = $this->fetchRow($select);
    	$date_now = new Zend_Date();
    	$date = new Zend_Date($res['date_close']);
    	if($date->compare($date_now) == -1){
    		return false;
    	}
    	else{
    		return true;
    	}
    }
	public function CheckClose($id){
    	$_id = (int)$id;
    	$select = $this->select();
    	$select->from($this->_name, 'status');
    	$select->where('id = ?', $_id);
    	$res = $this->fetchRow($select);
    	if($res->status == 'closed'){
    		return false;
    	}
    	else{
    		return true;
    	}
    }
    public function close($id, $score_left, $score_right){
        if($score_left > $score_right){
    		$result = 'left';
    	}
    	elseif($score_left < $score_right){
    		$result = 'right';
    	}
    	else{
    		$result = 'draw';
    	}
    	$data = array('score_left' => $score_left, 
    				'score_right' => $score_right, 
    				'result' => $result, 
    				'status' => 'closed');
    	if(!$this->update($data, 'id = ' . (int)$id))
    	{
    		return 0;
    	}
    	//$bets = $this->find($id)->current()->findDependentRowset('Application_Model_DbTable_Bet', 'Event');
    	$bet_tbl = new Application_Model_DbTable_Bet();
    	$bets = $bet_tbl->GetBetsById(null, null, $id);
    	foreach ($bets as $bet){
    		$bet_tbl->close($bet, $result, $score_left, $score_right);    		
    	}    	
    }
	public function deleteEvent($event_id){
		if(!$event_id){
			return 0;
		}
		$bet_tbl = new Application_Model_DbTable_Bet();
		$bets = $bet_tbl->GetBetsById(null, null, $event_id, null, false);
		foreach ($bets as $bet){
			$bet_tbl->deleteBet($bet->id);			
		}
		$where = 'id = ' . $event_id;
		$this->delete($where);
	}
}
